package grpc

import (
	"context"
	"share-works/rpc/models/dao"
	"share-works/rpc/models/services"
	user1 "share-works/rpc/proto/user"
)

type UserServer struct {
	user1.UnimplementedRegisterServerServer
}

func (s *UserServer) Register(ctx context.Context, req *user1.RegisterReq) (*user1.RegisterResp, error) {
	user := dao.User{
		Name: req.Name,
		Pass: req.Pass,
	}
	err := services.CreateUser(user)
	if err != nil {
		return &user1.RegisterResp{
			Code: 400,
			Msg:  "创建用户失败",
		}, err
	}
	resp := &user1.RegisterResp{
		Code: 200,
		Msg:  "创建用户成功",
	}
	return resp, err
}

func (s *UserServer) AddWorks(ctx context.Context, req *user1.AddWorksReq) (*user1.AddWorksResp, error) {
	works := dao.Works{
		Name:      req.Name,
		Videos:    req.Videos,
		Pictures:  req.Pictures,
		Introduce: req.Introduce,
	}
	err := services.CreateWorks(works)
	if err != nil {
		return &user1.AddWorksResp{
			Code: 400,
			Msg:  "添加作品失败",
		}, err
	}
	resp := &user1.AddWorksResp{
		Code: 200,
		Msg:  "添加作品成功",
	}
	return resp, err
}
